<?php
class PicturesForm extends BaseForm {
	private $albumId; 
	private $albumName; 
	
	private $selected; 
	private $pictures; 
	
	public function PicturesForm(){
		parent::BaseForm();
		
		$this->pictures = array();
		$this->selected = array();
	}
	
	public function setPictures($val){ $this->pictures = $val; }
	public function setAlbumId($val){ $this->albumId = $val; }
	public function setAlbumName($val){ $this->albumName = $val; }
	public function setSelected($val){ $this->selected = $val; }
	
	public function getPictures(){ return $this->pictures; }
	public function getAlbumId(){ return $this->albumId; }
	public function getAlbumName(){ return $this->albumName; }
	public function getSelected(){ return $this->selected; }
	
	
	public function clear(){
		$this->setPictures(array());
		$this->setSelected(array());
		$this->setAlbumId(null);
		$this->setAlbumName(null);
	}
	
	public function addPicture($picture){
		$this->pictures[] = $picture;
	}
	
	public function updatePicture($picture){
		$id = $picture->getId();
		
		$selected = $this->findPicture($id);
		if($selected != null){
			$selected->copy($picture);
		}
	}
	
	public function findPicture($id){
		$res = null;
		
		foreach($this->getPictures() as $current){
			if($id == $current->getId()){
				$res = $current;
				
				break;
			}
		}
		
		return $res;
	}
	
	public function deletePictures($ids){
		if(count($ids) > 0){
			$flip = array_flip($ids);
			$other = array();
			
			foreach($this->getPictures() as $current){
				$id = $current->getId();
				
				if(! array_key_exists($id, $flip)){
					$other[] = $current;
				}
			}
			
			$this->setPictures($other);
		} // if count $ids
	}
	
	public function getSelectedPictures($selected){
		$arr = array();
		
		if(is_array($selected)){
			$flip = array_flip($selected);
			
			foreach($this->pictures as $current){
				$id = $current->getId();
				
				if(array_key_exists($id, $flip)){
					$arr[] = $current;
				}
			}
		}
		
		return $arr;
	}
} 
?>